package com.vue.admin.service.impl;

import com.vue.admin.entity.SysRole;
import com.vue.admin.entity.SysRoleExample;
import com.vue.admin.mapper.SysRoleMapper;
import com.vue.admin.service.SysRoleService;
import com.vue.admin.util.StringUtil;
import com.vue.admin.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class SysRoleServieImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    public List<SysRole> selectByCondition(SysRoleExample example) {
        return sysRoleMapper.selectByExample(example);
    }

    public List<SysRole> selectRoleByUserId(String userId) {
        return sysRoleMapper.selectRoleByUserId(userId);
    }

    @Transactional
    public boolean save(SysRole sysRole) {
        if (StringUtil.isEmpty(sysRole.getId())) {
            sysRole.setCreateTime(new Date());
            sysRole.setId(UUIDUtil.uuidRandom());
            return sysRoleMapper.insert(sysRole) > 0;
        } else {
            sysRole.setUpdateTime(new Date());
            return sysRoleMapper.updateByPrimaryKey(sysRole) > 0;
        }
    }


    @Transactional
    public boolean delete(String id) {
        return sysRoleMapper.deleteByPrimaryKey(id) > 0;
    }
}
